package com.example.passport.controller;


import com.example.common.dto.JsonResult;
import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.interceptor.UserContext;
import com.example.passport.dto.RoleCreateDto;
import com.example.passport.dto.RoleDto;
import com.example.passport.dto.RoleSearchDto;
import com.example.passport.entity.Role;
import com.example.passport.entity.User;
import com.example.passport.service.RoleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "角色管理")
@RestController
@CrossOrigin
@RequestMapping("/passport/role")
public class RoleController {

    @Resource
    public RoleService roleService;

    @ApiOperation("添加角色（小组）")
    @PostMapping("/create")
    public JsonResult<String> create(@Valid RoleCreateDto roleCreatDto) {
        roleService.createRole(roleCreatDto);
        return new JsonResult<>();
    }

    @ApiOperation("列出所有角色（小组）")
    @GetMapping("/list")
    public JsonResult<ListDto<RoleDto>> listRole(RoleSearchDto condition, Pagination pagination) {
        condition.setTenantId( UserContext.getUser().getTenantId());
        ListDto<RoleDto> roleDtoList = roleService.listRole(condition, pagination);
        return new JsonResult<>(roleDtoList);
    }

    @ApiOperation("修改数据")
    @PostMapping("/update")
    public JsonResult<String> update(@Valid RoleDto roleDto) {
        roleService.update(roleDto);
        return new JsonResult<>();
    }

    @ApiOperation("修改数据")
    @PostMapping("/updateRoleMenu")
    public JsonResult<String> updateRoleMenu(@Valid RoleDto roleDto) {
        roleService.update(roleDto);
        return new JsonResult<>();
    }

    @ApiOperation("删除数据")
    @PostMapping("/delete")
    public JsonResult<String> delete(long id) {
        roleService.delete(id);
        return new JsonResult<>();
    }

}
